/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.widgetcontainer.client.presenter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPBinder;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPShell;
import fr.openparts.OpenParts.client.OPSlot;
import fr.openparts.OpenParts.client.OPSlotProxy;
import fr.openparts.OpenParts.client.annotations.OPServiceName;
import fr.openparts.OpenParts.client.annotations.OPViewFactory;
import fr.openparts.opshowcase.widgetcontainer.client.view.WidgetContainerView;

/**
 * Presenter of WidgetContainer, for OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 06 May 2011
 */
@OPServiceName("widgetcontainer")
public class WidgetContainerPresenter implements EntryPoint {

    public interface WidgetContainerPresenterBinder extends OPBinder<WidgetContainerPresenter> {}
    public final static WidgetContainerPresenterBinder binder = GWT.create(WidgetContainerPresenterBinder.class);
    
    public static final String EMBEDDED = "widgetembedded";
    
    protected WidgetContainerView view;
    protected OPSlot slot;
    
    /**
     * Entry point, method called when the module is loaded.
     * 
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    @Override
    public void onModuleLoad() {
        binder.bindPart(this);
        slot = OPShell.getSlot(EMBEDDED, EMBEDDED, OPShell.ACTION_DISPLAY);
        view = new WidgetContainerView(this);
        binder.notifyLoadingCompleted();
    }
    
    /**
     * Returns the view for this module
     * 
     * @param params
     *            not used here
     * @return the view for this module
     */
    @OPViewFactory(resourceName = "widgetcontainer", actionName = OPShell.ACTION_DISPLAY)
    public Widget getEmbeddedView(final OPParams params, final OPSlotProxy proxy) {
        return view;
    }

    /**
     * Display an embedded view in the given view.
     */
    public void displayEmbeddedView(final WidgetContainerView container) {
        container.setContent(slot);
        slot.add(new Label("Loading..."));
        
        // wait a bit so that we have time to see the loading message
        final Timer sleeper = new Timer() {
            public void run() {
                OPShell.displayView(EMBEDDED, EMBEDDED, OPShell.ACTION_DISPLAY, slot);
            }
        };
        sleeper.schedule(500);
    }
    
    // calling proxy commands on the embedded view
    
    public void setStyleName() {
        slot.getViewProxy().setStyleName("uniqueStyle");
    }

    public void addStyleName() {
        slot.getViewProxy().addStyleName("additionalStyle");
    }

    public void removeStyleName() {
        slot.getViewProxy().removeStyleName("additionalStyle");        
    }

    public void setStylePrimaryName() {
        slot.getViewProxy().setStylePrimaryName("primaryStyle");
    }

    public void addStyleDependentName() {
        slot.getViewProxy().addStyleDependentName("selected");        
    }

    public void removeStyleDependentName() {
        slot.getViewProxy().removeStyleDependentName("selected");
    }

    public void setWidth() {
        slot.getViewProxy().setWidth("100px");
    }

    public void setHeight() {
        slot.getViewProxy().setHeight("100px");
    }

    public void setSize() {
        slot.getViewProxy().setSize("200px", "250px");
    }

    public void setVisible(final boolean visible) {
        slot.getViewProxy().setVisible(visible);
    }

}
